/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jjsoft.celularessuc.gco.ui;

import com.jjsoft.celularessuc.gco.mdl.Articulo;
import com.jjsoft.celularessuc.gco.mdl.Articulosucursal;
import com.jjsoft.celularessuc.gco.mdl.Sucursal;
import com.jjsoft.celularessuc.gco.neg.ArticuloManager;
import com.jjsoft.celularessuc.gco.neg.ArticuloSucursalManager;
import com.jjsoft.celularessuc.gco.neg.SucursalManager;
import com.jjsoft.celularessuc.gco.util.MiModelo;
import java.util.ArrayList;
import java.util.List;
import org.apache.avro.generic.GenericData;

/**
 *
 * @author Johnny
 */
public class VBuscarArticuloSucursales extends javax.swing.JFrame {

    /**
     * Creates new form VBuscarArticuloSucursales
     */
    List<Articulosucursal> listaArticulossucursales;
    ArticuloSucursalManager asm = new ArticuloSucursalManager();
    List<Articulo> listaArticulos;
    List<Sucursal> listaSucursales;

    public VBuscarArticuloSucursales() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mnuSeries = new javax.swing.JPopupMenu();
        mnuVerSeries = new javax.swing.JMenuItem();
        jLabel1 = new javax.swing.JLabel();
        txtMatch = new javax.swing.JTextField();
        cmdBuscar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        dtgArticulos = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();

        mnuVerSeries.setText("Ver números de serie");
        mnuVerSeries.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuVerSeriesActionPerformed(evt);
            }
        });
        mnuSeries.add(mnuVerSeries);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(51, 51, 51));
        jLabel1.setText("Buscar en la tabla:");

        txtMatch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtMatchActionPerformed(evt);
            }
        });
        txtMatch.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                txtMatchPropertyChange(evt);
            }
        });
        txtMatch.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtMatchKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtMatchKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtMatchKeyTyped(evt);
            }
        });

        cmdBuscar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        cmdBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/buscar.png"))); // NOI18N
        cmdBuscar.setText("  Buscar");
        cmdBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdBuscarActionPerformed(evt);
            }
        });

        dtgArticulos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        dtgArticulos.setComponentPopupMenu(mnuSeries);
        dtgArticulos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(dtgArticulos);

        jLabel2.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 0, 153));
        jLabel2.setText("Buscar un Artículo en todas las sucursales");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 744, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtMatch, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(cmdBuscar, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                            .addGap(239, 239, 239))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 724, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(12, 12, 12)
                            .addComponent(jLabel2)))
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel2)
                    .addGap(18, 18, 18)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(txtMatch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cmdBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap()))
        );

        setSize(new java.awt.Dimension(760, 339));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void txtMatchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtMatchActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtMatchActionPerformed

    private void txtMatchPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_txtMatchPropertyChange
        // TODO add your handling code here:
    }//GEN-LAST:event_txtMatchPropertyChange

    private void txtMatchKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMatchKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == 10) {
            cmdBuscar.doClick();
        }
    }//GEN-LAST:event_txtMatchKeyPressed

    private void txtMatchKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMatchKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_txtMatchKeyReleased

    private void txtMatchKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMatchKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_txtMatchKeyTyped

    private void cmdBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdBuscarActionPerformed
        // TODO add your handling code here:
        if (this.txtMatch.getText().length() > 0) {

            this.listaArticulossucursales = asm.buscarEnSucursales(txtMatch.getText());
            if (listaArticulossucursales != null) {
                ArticuloManager am = new ArticuloManager();
                SucursalManager sm = new SucursalManager();
                MiModelo modelo = new MiModelo();
                String[] identificadores = {"Cod", "Descipcion", "Marca", "Modelo", "Precio Venta", "Stock", "Sucursal"};
                modelo.setColumnIdentifiers(identificadores);
                listaArticulos = new ArrayList<Articulo>();
                listaSucursales = new ArrayList<Sucursal>();
                for (int i = 0; i < listaArticulossucursales.size(); i++) {
                    Articulo objArt = am.buscaPorId(listaArticulossucursales.get(i).getIdArticulo());
                    Sucursal objSuc = sm.buscaPorId(listaArticulossucursales.get(i).getIdSucursal());
                    String[] articulo = {objArt.getCodArticulo(), objArt.getDescripcion(), objArt.getMarca(), objArt.getModelo(), objArt.getPventa().toString(), String.valueOf(listaArticulossucursales.get(i).getStock()), objSuc.getNombre()};
                    modelo.addRow(articulo);
                    listaArticulos.add(objArt);
                    listaSucursales.add(objSuc);
                }
                this.dtgArticulos.setModel(modelo);
                this.dtgArticulos.getColumn("Cod").setPreferredWidth(100);
                this.dtgArticulos.getColumn("Descipcion").setPreferredWidth(200);
                this.dtgArticulos.getColumn("Marca").setPreferredWidth(120);
                this.dtgArticulos.getColumn("Modelo").setPreferredWidth(120);
                this.dtgArticulos.getColumn("Stock").setPreferredWidth(50);
            }
        }
    }//GEN-LAST:event_cmdBuscarActionPerformed

    private void mnuVerSeriesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuVerSeriesActionPerformed
        // TODO add your handling code here:
        if(dtgArticulos.getSelectedRow()!=-1)
        {
            new VVerSeriesDisponibles(listaArticulos.get(dtgArticulos.getSelectedRow()), listaSucursales.get(dtgArticulos.getSelectedRow())).show();
        }
    }//GEN-LAST:event_mnuVerSeriesActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VBuscarArticuloSucursales.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VBuscarArticuloSucursales.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VBuscarArticuloSucursales.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VBuscarArticuloSucursales.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VBuscarArticuloSucursales().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdBuscar;
    private javax.swing.JTable dtgArticulos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu mnuSeries;
    private javax.swing.JMenuItem mnuVerSeries;
    private javax.swing.JTextField txtMatch;
    // End of variables declaration//GEN-END:variables
}
